package com.atguigu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.entity.EduTeacher;
import com.atguigu.entity.vo.TeacherQuery;
import com.atguigu.exceptionhandler.GuliException;
import com.atguigu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wxc
 * @since 2021-06-29
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    //访问地址：http://localhost:8001/eduservice/teacher/findAll

    @Autowired
    private EduTeacherService eduTeacherService;

    //1、查询讲师表中的所有数据
    //rest风格
    @ApiOperation("所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        //调用service的方法实现查询的所有操作
        List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("items", list);
    }

    //2、逻辑删除讲师的方法
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id){
        boolean flag = eduTeacherService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    //3、分页查询
    @ApiOperation("分页查询讲师")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") Long current,
                             @PathVariable("limit") Long limit){
        //创建一个Page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        eduTeacherService.page(pageTeacher);
        long total = pageTeacher.getTotal();    //总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合

//        Map map = new HashMap();
//        map.put("total", total);
//        map.put("rows", records);
//        return R.ok().data(map);

        return R.ok().data("total", total).data("rows", records);
    }

    //4、条件查询带分类的方法
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建Page对象
        Page<EduTeacher> page = new Page<>(current, limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        //wrapper条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(name != null){
            wrapper.like("name", name);
        }
        if(level != null){
            wrapper.eq("level", level);
        }
        if(begin != null){
            wrapper.ge("gmt_create", begin);
        }
        if(end != null){
            wrapper.le("gmt_create", end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        eduTeacherService.page(page, wrapper);

        //调用方法实现条件查询分页
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    //添加讲师接口的方法
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody(required = false) EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        return flag ? R.ok() : R.error();
    }


    //根据讲师id进行查询
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    //讲师修改
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody(required = false) EduTeacher teacher){
        //System.out.println(teacher);
        boolean flag = eduTeacherService.updateById(teacher);
        //System.out.println(flag);
        return flag ? R.ok() : R.error();
    }
}

